package v1

import (
	"github.com/astaxie/beego/orm"
	"pq/helper"
	"pq/models"
	"pq/service"
	"strconv"
)

type UserController struct {
	TokenCheckController
}
func (this *UserController) HeartBeat(){
	this.Success("I'm alive")
}
func (this *UserController) Option(){
	this.Success("Option invoked")
}
func (this *UserController) Get() {
	skip,_  := this.GetInt("skip",0)
	limit,_ := this.GetInt("limit",models.MAXROWS)
	user := new(models.PqUser)
	q:= []interface{}{
		skip,limit,
	}
	users,count := user.ListAll(q)
	//count := models.GetTotal()
	this.Response(users,count)
}
func (this *UserController) Post() {
	halt := helper.RandString(16)
	user := &models.PqUser{}
	user.Id,_ = this.GetInt("id",0)
	user.Name = this.GetString("name","")
	password := this.GetString("password","")
	user.Nickname = this.GetString("nickname")
	user.Type,_ = this.GetInt("type")
	user.State,_ = this.GetInt("state")
	user.Avatar = this.GetString("avatar")
	user.PqTestCategoryId,_ = this.GetInt("pq_test_category_id")
	//d,_ := helper.Decrypt(user.Password,user.Salt)
	//beego.Debug("decrypt:"+d)
	if user.Id > 0 {
		master := models.GetMasterDb()
		if password != ""{
			user.Password , _ = helper.Crypt(password,halt)
			user.Salt = halt
			if _,err := master.Update(user);err == nil{
				this.Success("编辑成功")
			} else{
				this.Fail("编辑失败,原因:"+err.Error(),"")
			}
		}else {
			if _, err := master.Update(user, "Name", "Nickname", "Type", "State", "Avatar","PqTestCategoryId"); err == nil {
				this.Success("编辑成功")
			}else{
				this.Fail("编辑失败,原因:"+err.Error(),"")
			}
		}
	}else {
		user.Password , _ = helper.Crypt(password,halt)
		user.Salt = halt
		if user.Name == "" || user.Nickname == ""{
			this.Fail("名字或昵称未指定","")
		}
		if user.Create() {
			this.Success("创建成功!")
		}else{
			this.Fail("用户已存在!","")
		}
	}
	this.Fail("更新失败!","")
}
func (this *UserController) UpdateCategory(){
	userService := service.NewUserService()
	category_id,_ := this.GetInt("test_category_id")
	uid,_ := strconv.Atoi(this.Uid)
	_,err := userService.SetCategory(uid,category_id)
	if err != nil{
		this.Fail(err.Error(),"更新失败")
		return
	}
	this.Success("更新成功")
}

func (this *UserController) UpdateArea(){
	userService := service.NewUserService()
	areaCode := this.GetString("area_code")
	uid,_ := strconv.Atoi(this.Uid)
	_,err := userService.SetArea(uid,areaCode)
	if err != nil{
		this.Fail(err.Error(),"更新失败")
		return
	}
	this.Success("更新成功")
}
func (this *UserController) FrontUpdate(){

}
func (this *UserController) Del(){
	id := this.Ctx.Input.Param(":id")
	master := models.GetMasterDb()
	if _,err := master.QueryTable("pq_user").Filter("id",id).Update(orm.Params{
		"state" : -1,
	});err != nil{
		this.Fail(err.Error(),"")
		return
	}
	this.Success("Deleted Successfully")
}
func (this *UserController) GetInfo(){
	us := service.UserService{}
	uid,_ := strconv.Atoi(this.Uid)
	my,err := us.GetUserById(uid)
	if err != nil{
		this.Fail("用户不存在","")
		return
	}
	this.Single(my)
}

//unsafe method,make sure only support access in a local net environment
func (u *UserController) GetInfoByPhone() {
	us := service.UserService{}
	phone := u.GetString("phone","")
	if phone == "" {
		u.Fail("参数不合法","")
		return
	}
	user,_ := us.GetUserByPhone(phone)
	u.Single(user)
}

//unsafe method,make sure only support access in a local net environment
func (u *UserController) UpdateAreaCode(){
	userService := service.NewUserService()
	uid,_ := u.GetInt("uid")
	areaCode := u.GetString("area_code","")
	if uid < 0 || areaCode == "" {
		u.Fail("参数不合法","")
		return
	}
 	_,err := userService.SetArea(uid,areaCode)
	if err != nil{
		u.Fail(err.Error(),"")
		return
	}
	u.Success("更新成功")
}